package com.whiteape.book.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;

import com.gyf.immersionbar.ImmersionBar;
import com.umeng.analytics.MobclickAgent;
import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.ui.activity.MainActivity;
import com.whiteape.book.ui.activity.SearchActivity;
import com.whiteape.book.ui.base.BaseTabFragment;
import com.whiteape.book.utils.ShareUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class HomepageFragment extends BaseTabFragment {
    @BindView(R.id.card_search)
    CardView cardSearch;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.iv_search)
    ImageView ivSearch;

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
//        ImmersionBar.with(this)
//                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
//                .statusBarDarkFont(false)
//                .statusBarColor(R.color.colorAccent)
//                .init();

    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_homepage;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivSearch.setOnClickListener(v -> startActivity(new Intent(getContext(), SearchActivity.class)));
    }

    @Override
    protected List<Fragment> createTabFragments() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(HomepageChildFragment.newInstance(MediaType.getName(MediaType.MEDIA_TYPE_NOVEL)));
        fragmentList.add(HomepageChildFragment.newInstance(MediaType.getName(MediaType.MEDIA_TYPE_COMIC)));

        return fragmentList;
    }

    @Override
    protected List<String> createTabTitles() {
        return Arrays.asList(MediaType.getName(MediaType.MEDIA_TYPE_NOVEL), MediaType.getName(MediaType.MEDIA_TYPE_COMIC));
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
//        mTlIndicator.setSelectedTabIndicatorColor(WaApp.getInstance().getResources().getColor(R.color.colorAccent));
//        mTlIndicator.setTabTextColors(ColorUtil.isColorLight(ThemeStore.primaryColor(WaApp.getInstance().getApplicationContext())) ? Color.BLACK : Color.WHITE,
//                ThemeStore.accentColor(MApplication.getInstance().getApplicationContext()));
        cardSearch.setOnClickListener(view -> {
            MobclickAgent.onEvent(getContext(), "homepage_search");
            startActivityByAnim(new Intent(getContext(), SearchActivity.class),
                    cardSearch, "sharedView", android.R.anim.fade_in, android.R.anim.fade_out);
        });
        ivShare.setOnClickListener(view -> {

            MobclickAgent.onEvent(getContext(), "homepage_share");
            ShareUtil.checkPermissionAndShareInFragment(this, (MainActivity) getActivity());
//            checkPermissionAndShare();
//            ShareUtil.umShare(getActivity(), ((MainActivity) getActivity()));
        });
        if (getActivity() != null && getActivity().getTheme() != null) {
            VectorDrawableCompat vectorDrawableCompat = VectorDrawableCompat.create(getResources(),
                    R.drawable.ic_share1, getActivity().getTheme());
            if (vectorDrawableCompat != null) {
                vectorDrawableCompat.setTint(getResources().getColor(R.color.share_btn_bg));
                ivShare.setImageDrawable(vectorDrawableCompat);
            }
        }
    }

}

